package com.resttemplatetest.controller;

import com.alibaba.fastjson.JSONObject;
import com.resttemplatetest.entity.DeviceUpgradeRequest;
import com.resttemplatetest.result.Result;
import org.junit.Test;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Hanson
 * @date 2022/11/29 15:39
 */
@RestController
public class TestController {

//    @Autowired
//    private RestTemplate restTemplate;

//    @Resource
//    LoadBalancerClient loadBalancedClient;   // 注入LoadBalancerClient

    /**
     * 第一种方式，这种方式只要指定url和返回类型即可调用，但是url是写死的，非常不方便。
     * @return
     */
    @GetMapping("/getMsg1")
    public String getMsg1(){
        RestTemplate restTemplate=new RestTemplate();
        String response = restTemplate.getForObject("http://localhost:7080/msg",String.class);// 调用restTemplate中的msg接口
        return response;
    }

    /**
     * get方法
     */
    @Test
    public void  getMsgTest(){
//        List<DeviceUpgradeRequest> objectArrayList = new ArrayList<DeviceUpgradeRequest>();
//        DeviceUpgradeRequest deviceUpgradeRequest = new DeviceUpgradeRequest();
//        deviceUpgradeRequest.setDid("20222021");
//        deviceUpgradeRequest.setFileName("ota_image_hcr.bin");
//        objectArrayList.add(deviceUpgradeRequest);
        RestTemplate restTemplate = new RestTemplate();

        String fileUploadUrl = "http://localhost:8140/ota/deviceUpgrade";
        // 封装file文件
        Map<String, Object> params = new HashMap<>();

        params.put("did","20222021");
        params.put("fileName","ota_image_hcr.bin");
        params.put("abbreviation","HCR");
        params.put("topic","OTA/D");

        String s = restTemplate.postForObject(fileUploadUrl, params, String.class);// 调用restTemplate中的msg接口
        JSONObject object = JSONObject.parseObject(JSONObject.parseObject(s).getString("data"));
        System.out.println(object);
    }

    /**
     * post方法
     */
    @RequestMapping("/upload")
    public String upload(MultipartFile file) throws IOException {
        RestTemplate restTemplate = new RestTemplate();
        String url = "http://localhost:7082/minio/uploadFile";
        MultiValueMap<String,Object> params = new LinkedMultiValueMap<>();
        ByteArrayResource fileAsResource = new ByteArrayResource(file.getBytes()) {
            @Override
            public String getFilename() {
                return file.getOriginalFilename();
            }
            @Override
            public long contentLength() {
                return file.getSize();
            }
        };
        params.add("file", fileAsResource);
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.MULTIPART_FORM_DATA);
        HttpEntity<MultiValueMap<String,Object>> requestEntity  = new HttpEntity<>(params, headers);
        Result<String> result = restTemplate.postForObject(url,requestEntity ,Result.class);
        return result.getData();
    }


    /**
     * 当我们的服务有多个URL的时候，第一种方式肯定是不行的，所以第二种方式是使用Spring Cloud提供的LoadBalancerClient
     * LoadBalancerClient资料
     * https://blog.csdn.net/qq_31142237/article/details/90512327
     *
     * @return
     */
//    @GetMapping("/getMsg2")
//    public String getMsg2(){
//        RestTemplate restTemplate=new RestTemplate();
//        ServiceInstance serviceInstance=loadBalancedClient.choose("MSG");
//        String url=String.format("http://%s:%s",serviceInstance.getHost(),serviceInstance.getPort());
//        String response=restTemplate.getForObject(url,String.class);
//        return response;
//    }

    /**
     * @return
     */
//    @GetMapping("/getMsg3")
//    public String getMsg3(){
//        String response=restTemplate.getForObject("http://MSG/msg",String.class);
//        return response;
//    }

}
